package com.wiscamp.ninechapters.problems.domain.models.sets;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.wiscamp.ninechapters.ddd.AggregateRoot;
import lombok.Data;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 习题集
 */
@Data
public class ProblemSet implements AggregateRoot {
    @Serial
    private static final long serialVersionUID = -348192187589426292L;

    /**
     * 习题集编号
     */
    private long setId = 0;

    /**
     * 习题集代码
     */
    private String setCode = "";

    /**
     * 习题集名称
     */
    private String setName = "";

    /**
     * 习题集类型
     */
    private int setTypeId = 0;

    /**
     * 关联编号
     */
    private long referenceId = 0;

    /**
     * 规则编号
     */
    private long ruleId = 0;

    /**
     * 机构编号
     */
    private long organizationId;

    /**
     * 学科编号
     */
    private long subjectId = 0;

    /**
     * 总题数
     */
    private int problemQuantity = 0;

    /**
     * 题分数
     * 为所有辖题的总分数，每题分数为难度值整除10，最低分数为1
     */
    private double totalScore = 0;

    /**
     * 答题时限
     * 每题做题时间为5分钟，总题数乘5
     */
    private int totalTimeLimit = 0;

    /**
     * 是否真题
     */
    private boolean trueProblem = false;

    /**
     * 难度系数
     * 所有辖题的平均难度值
     */
    private int difficulty = 0;

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 是否可用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 创建用户
     */
    private long creatorId = 0;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    private String collectionName = "";

    private String setTypeName = "";

    private String creatorName = "";

    private String modifierName = "";

    /**
     * 习题集中的题列表
     */
    private Collection<SetProblem> setProblems = new ArrayList<>();

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public Collection<Long> getProblemIds() {
        return this.setProblems.stream().map(SetProblem::getProblemId).collect(Collectors.toList());
    }

    public int parseSetYear() {
        var setName = this.setName;
        int index = setName.lastIndexOf("-");
        String temp = setName.substring(0, index);
        temp = temp.replace("-", "").replace("AMC8 ", "");
        temp = temp.replace("AMC10A ", "").replace("AMC10B ", "");
        temp = temp.replace("AMC12A ", "").replace("AMC12B ", "");
        int year = Integer.parseUnsignedInt(temp);
        return year;
    }
}